<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :option="option"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :search.sync="searchForm"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="refreshChange"
        @on-load="refreshChange"
	:row-style="rowStyle"
      >
        <template slot="menuLeft">
          <el-popover
            v-if="permissions.stm_swstcycleapply_audit"
            placement="bottom"
            width="260"
            v-model="batchReviewVisible"
          >
            <div style="text-align: center; margin: 0">
              <el-form>
                <el-form-item label="异常通知内容">
                  <el-input type="textarea" span="24" v-model="diffCase"></el-input>
                </el-form-item>
              </el-form>
              <el-button size="mini" type="primary" @click="MyConfirm()">通知</el-button>
              <el-button type="primary" size="mini" @click="batchReviewVisible=false">取消</el-button>
            </div>
            <el-button type="primary" size="small" :hidden="true" v-if="false" slot="reference">通知</el-button>
          </el-popover>
          <el-button type="primary"  v-if="permissionList.delBtn"  size="small"  @click="handleDelete()">高级删除</el-button>
          <el-button type="primary" v-if="permissionList.delBtn" size="mini" @click="addReportData">加入报表差错</el-button>
          <el-button type="primary" size="small" v-if="false" @click="handleExport">导出</el-button>
          <el-button type="primary" size="small" :hidden="true" v-if="false" @click="handlePrint">打印</el-button>
          <addReportEditor ref="addReportEditor" @addReportSuccess="getList"></addReportEditor>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {fetchList, confirmObj ,delBatch} from '@/api/subway/swststockaudit'
import {nowDate, tableOption} from "@/const/crud/subway/swststockaudit";
import reportMistakeInfoEditor from '@/views/subwayRat/reportMondify/reportMistakeInfo/reportMistakeInfoEditor';
import addReportEditor from "@/views/subwayRat/reportMondify/reportMistakeInfo/Editor.vue";

export default {
  name: 'swststockaudit',
  components: {addReportEditor, reportMistakeInfoEditor},
  mixins: [baseCrud('subway/swststockaudit')],
  data() {
    return {
      option: tableOption,
      batchReviewVisible: false,
      diffCase: '',
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 5, // 每页显示多少条
        pageSizes:[5,10,20,30,40,50,100]
      },
      searchForm: {
        statisticsDate: nowDate,
        lineCode: '9'
      }
    }
  },
  computed: {
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.swststock_add, false),
        delBtn: this.vaildData(this.permissions.swststock_superdel, false),
        editBtn: this.vaildData(this.permissions.swststock_edit, false)
      };
    }
  },
  methods: {
    refreshChange() {
      this.getList()

    },
    rowStyle({ row, column, rowIndex }) {
      if (row.confirmStatus == "0") {
        return {
          backgroundColor: "#eee",
          color: "red",
        };
      }
    },
    getList() {
      const params = this.$route.query;
      console.log(JSON.stringify(params))
      const p = {};
      if (Object.keys(params).length) {
        p.lineCode = params.lineCode;
        p.stationCode = params.stationCode;
        p.statisticsDate = params.reprotTime;
      }
      this.listLoading = true;
      this.page.size = this.page.pageSize;
      this.page.current = this.page.currentPage;
      Object.assign(this.searchForm, this.page);
      this.searchForm.userCode = this.$store.getters.userInfo.userCode;
      fetchList(Object.keys(params).length ? p : this.searchForm).then((response) => {
        this.tableData = response.data.data.records;
        this.listLoading = false;
        this.page.total = response.data.data.total;
      });
    },
    selectionChange(list) {
      this.multipleSelectionRows = list;
      this.multipleSelectionKeys = [];
      for (let i = 0; i < list.length; i++) {
        this.multipleSelectionKeys.push(list[i].id);
      }
    },
    handleFilter(params, done) {
      Object.assign(this.searchForm, params);
      this.getList()
      done()
    },
    search() {
      this.listLoading = true;
      this.searchForm.pageType = 1;
      this.searchForm.checkStatus = 3;
      this.page.size = this.page.pageSize;
      this.page.current = this.page.currentPage;
      Object.assign(this.searchForm, this.page);
      this.searchForm.userCode = this.$store.getters.userInfo.userCode;
      this.searchForm.reportType = 1;
      //日期范围
      let dates1 = this.searchForm.dates1;
      if (dates1 != null && dates1 != '') {
        this.searchForm.startdate = dates1[0].substr(0, 10);
        this.searchForm.enddate = dates1[1].substr(0, 10);
      }
      fetchList(this.searchForm).then((res) => {
        this.tableData = res.data.data.records;
        this.listLoading = false;
        this.page.total = res.data.data.total;
      });
    },
    //删除多条数据
    //高级删除操作
    handleDelete: function () {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据！");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delBatch(ids)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {
            loading();
          });
      });
    },
    async addReportData() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      if (this.$refs.crud.tableSelect.length != 1) {
        this.$message.error("只能选择一条数据");
        return false;
      }
      let res = false;
      let ress = false;
      for (const table of this.$refs.crud.tableSelect) {
        if (table.confirmStatus != "1") {
          this.$message.error("请选择已提交的数据");
          return false;
        } else {
          if (table.isAddReprotError == "1") {
            res = await this.$confirm('该数据已加入报表差错，是否重新加入？', '提示', {
              confirmButtonText: '是',
              cancelButtonText: '否',
              type: 'warning'
            });
            if (!res) {
              return;
            } else {
              ress = true;
            }
          }
        }
      }
      if (res == false) {
        ress = await this.$confirm("是否加入报表差错", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        });
      }
      if (!ress) {
        return;
      }
      this.selectKeys = this.$refs.crud.tableSelect;
      let param = {
        dialogTitle: "加入报表差错",
        diffAmountId: this.selectKeys[0].id,
        dataSourceTable: "sw_st_stock",
        lineCode: this.selectKeys[0].lineCode,
        stationCode: this.selectKeys[0].stationCode,
        reprotTime: this.selectKeys[0].statisticsDate,
        reportNameId: 12,
        settleBeginTime: this.selectKeys[0].statisticsDate,
        settleEndTime: this.selectKeys[0].statisticsDate,
	userCodes:this.selectKeys[0].confirmPersonJobNo,
	dutyStationLeaderCode: this.selectKeys[0].confirmPersonJobNo,
	dutyStationLeaderName: this.selectKeys[0].conformPersonName,
        showType: 3,
        dataSource: 13
      };
      this.$refs.addReportEditor.others_report_add_call(param);
    },
    MyConfirm() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        table.diffCase = this.diffCase;
        ids.push(table)
      }
      confirmObj(ids).then(data => {
        this.$message.success('通知成功');
        this.batchReviewVisible = false;
        this.getList(this.page);
      })
    }
  }
}
</script>
